var mainStartCode ='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
function initPage(baseInfoJson,diseaseDiagInfoJson){
	var basePath = $("#basePath").val();
	var diagTypes = baseInfoJson['diagTypes'];//诊断类型
	
	var treatmentResults = baseInfoJson['treatmentResults'];//治疗结果
	
	
	var diseaseDiagRecords = diseaseDiagInfoJson['diseaseDiagRecords'];//诊断记录集合
	
	
	$("select").each(function(){
		$(this).append("<option value=''>---请选择---</option>");
		
	});
	
	$(".diagTypeSelect").each(function(){
		var options = "";
		for(var index in diagTypes){
			var diagType = diagTypes[index];
	    	options+="<option value='"+diagType.uniq_code+"'>"+diagType.label+"</option>"
	    } 
       $(this).append(options);
       
	});
	
	$(".treatResultSelect").each(function(){
		var options = "";
		for(var index in treatmentResults){
			var treatmentResult = treatmentResults[index];
	    	options+="<option value='"+treatmentResult.uniq_code+"'>"+treatmentResult.label+"</option>"
	    } 
       $(this).append(options);
       
	});
	
	
	$(".operateMark").each(function(){
		options = "<option value='0'>非手术治疗</option>";
		
		options+="<option value='1'>手术治疗</option>";
			$(this).append(options);
		
	});
	
	for(var index in diseaseDiagRecords){
		var diseaseDiagRecord = diseaseDiagRecords[index];
		//console.log(diseaseDiagRecord);
		addDiseaseDiagRecordByDiag(diseaseDiagRecord);
    } 
	
	if($("#diseaseDiagTable tbody tr").length == 0){
		addDiseaseDiagRecord();
	}
	
	
};

function addDiseaseDiagRecord(){
	var basePath = $("#basePath").val();
	var diseaseDiagCountOjb = $("#diseaseDiagCount");
	var count = parseInt(diseaseDiagCountOjb.val());
	var add_content=$("#templateDiseaseDiagTr").clone();
	add_content.removeAttr("id");
	
	var diagTypeCodeOjb= add_content.find("select[name='diseaseDiagRecords[0].diagTypeCode']");
	diagTypeCodeOjb.attr("name","diseaseDiagRecords["+count+"].diagTypeCode");
	
	add_content.find("input[name='diseaseDiagRecords[0].diagTypeName']").attr("name","diseaseDiagRecords["+count+"].diagTypeName");
	
	add_content.find("input[name='diseaseDiagRecords[0].diseaseDiagOriginalDesc']").attr("name","diseaseDiagRecords["+count+"].diseaseDiagOriginalDesc");
	
	add_content.find("input[name='diseaseDiagRecords[0].diseaseDiagCode']").attr("name","diseaseDiagRecords["+count+"].diseaseDiagCode");
	add_content.find("input[name='diseaseDiagRecords[0].diseaseDiagName']").attr("name","diseaseDiagRecords["+count+"].diseaseDiagName");
	
	
	add_content.find("select[name='diseaseDiagRecords[0].treatResultCode']").attr("name","diseaseDiagRecords["+count+"].treatResultCode");
	add_content.find("input[name='diseaseDiagRecords[0].treatResultName']").attr("name","diseaseDiagRecords["+count+"].treatResultName");
	
	add_content.find("input[name='diseaseDiagRecords[0].confirmedDateTime']").attr("name","diseaseDiagRecords["+count+"].confirmedDateTime");
	add_content.find("input[name='diseaseDiagRecords[0].treatDayNumber']").attr("name","diseaseDiagRecords["+count+"].treatDayNumber");
	add_content.find("input[name='diseaseDiagRecords[0].diagSortIndex']").attr("name","diseaseDiagRecords["+count+"].diagSortIndex");
	add_content.find("select[name='diseaseDiagRecords[0].operateMarkCode']").attr("name","diseaseDiagRecords["+count+"].operateMarkCode");
	add_content.find("input[name='diseaseDiagRecords[0].operateMarkName']").attr("name","diseaseDiagRecords["+count+"].operateMarkName");
	
	$.each(add_content.find(".diagTypeSelect"),function(i,item) {
		$(item).change(function(){
	   		var eventObj = $(this);
	   		var nameObjName = eventObj.attr("name").replace("Code", "Name");
	   		
	   		if(eventObj.val()!=""){
	   			var name=eventObj.find("option:selected").text();
	   			eventObj.next("input[name='"+nameObjName+"']").val(name);
	   		}else{
	   			eventObj.next("input[name='"+nameObjName+"']").val("");
	   		}
	   	});
		
	});
	
	$.each(add_content.find(".diseaseDiagSearchInput"),function(i,item) {
		$(item).autocomplete({
			source: function( request, response ) {
				var eventObj = $(this.element);
				
				var codeObjName = eventObj.attr("name").replace("Name", "Code");
				eventObj.next("input[name='"+codeObjName+"']").val("");
				/* eventObj.next("input[data-code='search_input1']").val(""); */
				$.ajax({
					url: basePath + "base_info/ajax_diseases_by_query_name",
					dataType: "json",
					data:{
						queryName: request.term
					},
					success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label:item.label,
							uniq_code:item.uniq_code
						}
					}));
				}
			});
			},
			minLength: 1,
			select: function( event, ui ) {
				var eventObj = $(this);
				var item = ui.item;
				var codeObjName = eventObj.attr("name").replace("Name", "Code");
				
				eventObj.next("input[name='"+codeObjName+"']").val(item.uniq_code);
				/* eventObj.next("input[data-code='search_input1']").val(item.code); */
			},search: function( event, ui ) {
				/* eventObj.next("input[data-code='search_input1']").val(""); */
				var eventObj = $(this);
				var codeObjName = eventObj.attr("name").replace("Name", "Code");
				eventObj.next("input[name='"+codeObjName+"']").val("");
			}
		});
	});
	
	$.each(add_content.find(".treatResultSelect"),function(i,item) {
		$(item).change(function(){
	   		var eventObj = $(this);
	   		var nameObjName = eventObj.attr("name").replace("Code", "Name");
	   		
	   		if(eventObj.val()!=""){
	   			var name=eventObj.find("option:selected").text();
	   			eventObj.next("input[name='"+nameObjName+"']").val(name);
	   		}else{
	   			eventObj.next("input[name='"+nameObjName+"']").val("");
	   		}
	   	});
		
	});
	
	$.each(add_content.find(".operateMark"),function(i,item) {
		$(item).change(function(){
	   		var eventObj = $(this);
	   		var nameObjName = eventObj.attr("name").replace("Code", "Name");
	   		
	   		if(eventObj.val()!=""){
	   			var name=eventObj.find("option:selected").text();
	   			eventObj.next("input[name='"+nameObjName+"']").val(name);
	   		}else{
	   			eventObj.next("input[name='"+nameObjName+"']").val("");
	   		}
	   	});
		
	});
	
	$("#diseaseDiagTable tbody").append(add_content);
	diseaseDiagCountOjb.val(count +1);
}

function addDiseaseDiagRecordByDiag(diseaseDiagRecord){
	
	var diagTypeCode =diseaseDiagRecord.diagTypeCode;
	console.log(diseaseDiagRecord);
	
	var dataContent = diseaseDiagRecord.dataContent;

	
	
	var basePath = $("#basePath").val();
	var diseaseDiagCountOjb = $("#diseaseDiagCount");
	var count = parseInt(diseaseDiagCountOjb.val());
	var add_content=$("#templateDiseaseDiagTr").clone();
	add_content.removeAttr("id");
	
	var diagTypeCodeOjb= add_content.find("select[name='diseaseDiagRecords[0].diagTypeCode']");
	diagTypeCodeOjb.attr("name","diseaseDiagRecords["+count+"].diagTypeCode");
	add_content.find("input[name='diseaseDiagRecords[0].diseaseDiagOriginalDesc']").attr("name","diseaseDiagRecords["+count+"].diseaseDiagOriginalDesc").val(diseaseDiagRecord.diseaseDiagOriginalDesc);
	add_content.find("input[name='diseaseDiagRecords[0].diagTypeName']").attr("name","diseaseDiagRecords["+count+"].diagTypeName ");
	
	if(dataContent){
		add_content.find("input[name='diseaseDiagRecords[0].diseaseDiagCode']").attr("name","diseaseDiagRecords["+count+"].diseaseDiagCode").val(diseaseDiagRecord.diseaseDiagCode);
		
		/*if(diseaseDiagRecord.diseaseDiagCode){*/
			add_content.find("input[name='diseaseDiagRecords[0].diseaseDiagName']").attr("name","diseaseDiagRecords["+count+"].diseaseDiagName").val(diseaseDiagRecord.diseaseDiagName).css({"border":"1px solid red"}).attr("data-content",dataContent).popover({placement:'right'});
		/*}*/
	}else{
		add_content.find("input[name='diseaseDiagRecords[0].diseaseDiagCode']").attr("name","diseaseDiagRecords["+count+"].diseaseDiagCode").val(diseaseDiagRecord.diseaseDiagCode);
		
		/*if(diseaseDiagRecord.diseaseDiagCode){*/
			add_content.find("input[name='diseaseDiagRecords[0].diseaseDiagName']").attr("name","diseaseDiagRecords["+count+"].diseaseDiagName").val(diseaseDiagRecord.diseaseDiagName);
		/*}*/
	}
	
	
	
	
	
	add_content.find("select[name='diseaseDiagRecords[0].treatResultCode']").attr("name","diseaseDiagRecords["+count+"].treatResultCode");
	add_content.find("input[name='diseaseDiagRecords[0].treatResultName']").attr("name","diseaseDiagRecords["+count+"].treatResultName");
	
	add_content.find("input[name='diseaseDiagRecords[0].confirmedDateTime']").attr("name","diseaseDiagRecords["+count+"].confirmedDateTime").val(diseaseDiagRecord.confirmedDateTime);
	add_content.find("input[name='diseaseDiagRecords[0].treatDayNumber']").attr("name","diseaseDiagRecords["+count+"].treatDayNumber").val(diseaseDiagRecord.treatDayNumber);
	add_content.find("input[name='diseaseDiagRecords[0].diagSortIndex']").attr("name","diseaseDiagRecords["+count+"].diagSortIndex").val(diseaseDiagRecord.diagSortIndex);
	add_content.find("select[name='diseaseDiagRecords[0].operateMarkCode']").attr("name","diseaseDiagRecords["+count+"].operateMarkCode");
	add_content.find("input[name='diseaseDiagRecords[0].operateMarkName']").attr("name","diseaseDiagRecords["+count+"].operateMarkName");
	
	$.each(add_content.find(".diagTypeSelect"),function(i,item) {
		$(item).change(function(){
	   		var eventObj = $(this);
	   		var nameObjName = eventObj.attr("name").replace("Code", "Name");
	   		
	   		if(eventObj.val()!=""){
	   			var name=eventObj.find("option:selected").text();
	   			eventObj.next("input[name='"+nameObjName+"']").val(name);
	   		}else{
	   			eventObj.next("input[name='"+nameObjName+"']").val("");
	   		}
	   	});
		if(diseaseDiagRecord.diagTypeCode){
			
			var selectOption = $(item).find("option[value='"+diseaseDiagRecord.diagTypeCode+"']")
			if(selectOption.length > 0){
				selectOption.attr("selected",true);
			}else{
				$(item).append("<option selected='selected' value='"+diseaseDiagRecord.diagTypeCode+"'>"+diseaseDiagRecord.diagTypeName+"</option>");
			}
			
		}
		;
		$(item).next("input[name='"+$(item).attr("name").replace("Code", "Name")+"']").val(diseaseDiagRecord.diagTypeName);
	});
	
	$.each(add_content.find(".diseaseDiagSearchInput"),function(i,item) {
		$(item).autocomplete({
			source: function( request, response ) {
				var eventObj = $(this.element);
				
				var codeObjName = eventObj.attr("name").replace("Name", "Code");
				eventObj.next("input[name='"+codeObjName+"']").val("");
				$.ajax({
					url: basePath + "base_info/ajax_diseases_by_query_name",
					dataType: "json",
					data:{
						queryName: request.term
					},
					success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label:item.label,
							uniq_code:item.uniq_code
						}
					}));
				}
			});
			},
			minLength: 1,
			select: function( event, ui ) {
				var eventObj = $(this);
				var item = ui.item;
				var codeObjName = eventObj.attr("name").replace("Name", "Code");
				eventObj.next("input[name='"+codeObjName+"']").val(item.uniq_code);
			},search: function( event, ui ) {
				var eventObj = $(this);
				var codeObjName = eventObj.attr("name").replace("Name", "Code");
				eventObj.next("input[name='"+codeObjName+"']").val("");
			}
		});
	});
	
	$.each(add_content.find(".treatResultSelect"),function(i,item) {
		$(item).change(function(){
	   		var eventObj = $(this);
	   		var nameObjName = eventObj.attr("name").replace("Code", "Name");
	   		
	   		if(eventObj.val()!=""){
	   			var name=eventObj.find("option:selected").text();
	   			eventObj.next("input[name='"+nameObjName+"']").val(name);
	   		}else{
	   			eventObj.next("input[name='"+nameObjName+"']").val("");
	   		}
	   	});
		if(diseaseDiagRecord.treatResultCode){
			
			var selectOption = $(item).find("option[value='"+diseaseDiagRecord.treatResultCode+"']")
			if(selectOption.length > 0){
				selectOption.attr("selected",true);
			}else{
				$(item).append("<option selected='selected' value='"+diseaseDiagRecord.treatResultCode+"'>"+diseaseDiagRecord.treatResultName+"</option>");
			}
			
		}
		;
		$(item).next("input[name='"+$(item).attr("name").replace("Code", "Name")+"']").val(diseaseDiagRecord.treatResultName);
	});
	$.each(add_content.find(".operateMark"),function(i,item) {
		$(item).change(function(){
	   		var eventObj = $(this);
	   		var nameObjName = eventObj.attr("name").replace("Code", "Name");
	   		
	   		if(eventObj.val()!=""){
	   			var name=eventObj.find("option:selected").text();
	   			eventObj.next("input[name='"+nameObjName+"']").val(name);
	   		}else{
	   			eventObj.next("input[name='"+nameObjName+"']").val("");
	   		}
	   	});
		if(diseaseDiagRecord.operateMarkCode){
			
				var selectOption = $(item).find("option[value='"+diseaseDiagRecord.operateMarkCode+"']")
				if(selectOption.length > 0){
					selectOption.attr("selected",true);
				}else{
					$(item).append("<option selected='selected' value='"+diseaseDiagRecord.operateMarkCode+"'>"+diseaseDiagRecord.operateMarkName+"</option>");
				}
				
			};
			$(item).next("input[name='"+$(item).attr("name").replace("Code", "Name")+"']").val(diseaseDiagRecord.operateMarkName);
		});
	$("#diseaseDiagTable tbody").append(add_content);
	diseaseDiagCountOjb.val(count +1);
	
}
function deleteDiseaseDiagRecord(obj){
	$(obj).parent().parent().remove();
}





